package com.ruoyi.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.IJiajiaIntegralRecordService;
import com.ruoyi.system.domain.JiajiaCharityActivities;
import com.ruoyi.system.domain.JiajiaCharityActivitiesEnroll;
import com.ruoyi.system.domain.JiajiaIntegralRecord;
import com.ruoyi.system.mapper.JiajiaCharityActivitiesEnrollMapper;
import com.ruoyi.system.mapper.JiajiaUserMapper;
import com.ruoyi.system.service.IJiajiaCharityActivitiesEnrollService;
import com.ruoyi.system.service.IJiajiaCharityActivitiesService;
import com.ruoyi.system.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 公益活动报名Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-10
 */
@Service
public class JiajiaCharityActivitiesEnrollServiceImpl implements IJiajiaCharityActivitiesEnrollService
{
    @Resource
    private JiajiaCharityActivitiesEnrollMapper jiajiaCharityActivitiesEnrollMapper;

    @Resource
    private IJiajiaIntegralRecordService integralRecordService;

    @Resource
    private IJiajiaCharityActivitiesService jiajiaCharityActivitiesService;

    @Resource
    private JiajiaUserMapper jiajiaUserMapper;

    /**
     * 查询公益活动报名
     *
     * @param id 公益活动报名主键
     * @return 公益活动报名
     */
    @Override
    public JiajiaCharityActivitiesEnroll selectJiajiaCharityActivitiesEnrollById(Long id)
    {
        return jiajiaCharityActivitiesEnrollMapper.selectJiajiaCharityActivitiesEnrollById(id);
    }

    /**
     * 查询公益活动报名列表
     *
     * @param jiajiaCharityActivitiesEnroll 公益活动报名
     * @return 公益活动报名
     */
    @Override
    public List<JiajiaCharityActivitiesEnroll> selectJiajiaCharityActivitiesEnrollList(JiajiaCharityActivitiesEnroll jiajiaCharityActivitiesEnroll){
        return jiajiaCharityActivitiesEnrollMapper.selectJiajiaCharityActivitiesEnrollList(jiajiaCharityActivitiesEnroll);
    }

    /**
     * 新增公益活动报名
     *
     * @param jiajiaCharityActivitiesEnroll 公益活动报名
     * @return 结果
     */
    @Override
    public int insertJiajiaCharityActivitiesEnroll(JiajiaCharityActivitiesEnroll jiajiaCharityActivitiesEnroll) {
        jiajiaCharityActivitiesEnroll.setUserId(TokenUtil.getUid());
        jiajiaCharityActivitiesEnroll.setCreateTime(DateUtils.getNowDate());
        JiajiaCharityActivitiesEnroll charityActivitiesEnroll=jiajiaCharityActivitiesEnrollMapper.select(TokenUtil.getUid(),jiajiaCharityActivitiesEnroll.getActivitiesId());
        if (ObjectUtil.isNotNull(charityActivitiesEnroll)){
            throw new RuntimeException("您已经报名过该活动");
        }
        return jiajiaCharityActivitiesEnrollMapper.insertJiajiaCharityActivitiesEnroll(jiajiaCharityActivitiesEnroll);
    }

    /**
     * 公益活动报名完成提交
     *
     * @param jiajiaCharityActivitiesEnroll 公益活动报名
     * @return 结果
     */
    @Override
    public int updateJiajiaCharityActivitiesEnroll(JiajiaCharityActivitiesEnroll jiajiaCharityActivitiesEnroll) {
        jiajiaCharityActivitiesEnroll.setUpdateTime(new Date());
        jiajiaCharityActivitiesEnroll.setStatus(1);
        return jiajiaCharityActivitiesEnrollMapper.updateJiajiaCharityActivitiesEnroll(jiajiaCharityActivitiesEnroll);
    }

    /**
     * 批量删除公益活动报名
     *
     * @param ids 需要删除的公益活动报名主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaCharityActivitiesEnrollByIds(String ids)
    {
        return jiajiaCharityActivitiesEnrollMapper.deleteJiajiaCharityActivitiesEnrollByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除公益活动报名信息
     *
     * @param id 公益活动报名主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaCharityActivitiesEnrollById(Long id)
    {
        return jiajiaCharityActivitiesEnrollMapper.deleteJiajiaCharityActivitiesEnrollById(id);
    }

    @Override
    public int auditJiajiaCharityActivitiesEnroll(JiajiaCharityActivitiesEnroll jiajiaCharityActivitiesEnroll) {
        if (jiajiaCharityActivitiesEnroll.getStatus() == 2){
            JiajiaCharityActivitiesEnroll activitiesEnroll = jiajiaCharityActivitiesEnrollMapper.selectJiajiaCharityActivitiesEnrollById(jiajiaCharityActivitiesEnroll.getId());
            JiajiaCharityActivities jiajiaCharityActivities = jiajiaCharityActivitiesService.selectJiajiaCharityActivitiesById(activitiesEnroll.getActivitiesId());
            JiajiaIntegralRecord integralRecord = new JiajiaIntegralRecord();
            integralRecord.setUserId(jiajiaCharityActivitiesEnroll.getUserId());
            integralRecord.setIntegral(Long.valueOf(jiajiaCharityActivities.getActivityPoints()));
            integralRecord.setType(0);
            integralRecord.setManner(0);
            integralRecordService.insertJiajiaIntegralRecord(integralRecord);
            jiajiaUserMapper.updateJiajiaUserById(activitiesEnroll.getUserId(), jiajiaCharityActivities.getActivityPoints());
        }
        int i = jiajiaCharityActivitiesEnrollMapper.auditJiajiaCharityActivitiesEnroll(jiajiaCharityActivitiesEnroll);
        return i;
    }
}
